//
//  ZCFriendView.m
//  zctx
//
//  Created by apple on 16/4/8.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCFriendView.h"

@interface ZCFriendView()

@end

@implementation ZCFriendView


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.isshow = YES;
        self.userInteractionEnabled = YES;
//        self.backgroundColor = [UIColor orangeColor];
        self.frame = CGRectMake(SCREEN_WIDTH-106,SCREEN_HEIGHT-75-60,106,42);
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.bounds];
        imageView.image = [UIImage imageNamed:@"main_friend"];
        [self addSubview:imageView];
        
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gestureRecognizer)];
        [self addGestureRecognizer:gesture];
        
        
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(gotoDisAnimation) userInfo:nil repeats:YES];
        
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    }
    return self;
}

-(void)gestureRecognizer{
    
    if ([self.timer isValid]) {
        [self.timer invalidate];
        //这行代码很关键
        self.timer=nil;
    }
    if (self.isshow == NO) {
        [self showAnimation];
    }
    if (self.isshow == YES) {
        self.friendBlock();
    }
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(gotoDisAnimation) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    
}

-(void)gotoDisAnimation{
    if (self.isshow == YES) {
        [self dissAnimation];
    }
}

#pragma mark 隐藏动画
-(void)dissAnimation{
    [UIView animateWithDuration:0.3 delay:1 options:UIViewAnimationOptionCurveLinear animations:^{
        CGRect frame = self.frame;
        frame.origin.x = SCREEN_WIDTH-37;
        self.frame = frame;
    } completion:^(BOOL finished) {
        self.isshow = NO;
    } ];
}

#pragma mark 显示动画
-(void)showAnimation{
    [UIView animateWithDuration:0.3 delay:0.01 options:UIViewAnimationOptionCurveLinear animations:^{
        CGRect frame = self.frame;
        frame.origin.x = SCREEN_WIDTH-106;
        self.frame = frame;
    } completion:^(BOOL finished) {
        self.isshow = YES;
    } ];
}


@end
